import {fileURLToPath, URL} from 'node:url'
import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
// 自动导入Icon图标
import IconsResolver  from 'unplugin-icons/resolver'
import Icons from 'unplugin-icons/vite'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command })=> {
    const env = loadEnv(mode, process.cwd())
    return {
        base: command === 'build' ? './' : env.VITE_PUBLIC_PATH,
        plugins: [
            vue(),
            AutoImport({
                // 组件自动导入
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [
                    // 组件自动导入
                    ElementPlusResolver(),
                    // 自动注册图标组件
                    IconsResolver({
                        // 修改Icon组件前缀，不设置则默认为i,禁用则设置为false
                        prefix: 'icon',
                        // 指定collection，即指定为elementplus图标集ep
                        enabledCollections: ['ep']
                    })
                ],
            }),
            Icons({
                autoInstall: true
            })
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            }
        },
        server: {
            proxy: {
                [env.VITE_API_BASE_URL]: {
                    target: 'http://localhost:8134', // 目标接口域名
                    changeOrigin: true, // 是否跨域
                    ws: true,  // 允许websocket代理
                    rewrite: (path) => path.replace(env.VITE_API_BASE_URL, "")
                }
            }
        }
    }
})
